package cn.yfj.basic.service.impl;

import cn.yfj.basic.Constant.BaseConstant;
import cn.yfj.basic.config.BusinessException;
import cn.yfj.basic.domain.RegisterDto;
import cn.yfj.basic.service.IVerifyCodeService;
import cn.yfj.basic.util.SmsUtils;
import cn.yfj.basic.util.StrUtils;
import cn.yfj.basic.util.VerifyCodeUtils;
import cn.yfj.user.domain.Logininfo;
import cn.yfj.user.mapper.LogininfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Service
public class VerifyCodeServiceImpl implements IVerifyCodeService {
    @Autowired //redis依赖注入
    RedisTemplate redisTemplate;
    @Autowired
    LogininfoMapper logininfoMapper;
    @Override
    public String imageCode(String key) {
        /*判断是否为空*/
        if(key.isEmpty()){
            throw new BusinessException("系统繁忙");
        }
        /*生成4位随机数*/
        String randomString = StrUtils.getComplexRandomString(4);
        /*保存到redis并设置过期时间为4分钟*/
        redisTemplate.opsForValue().set(key,randomString,4, TimeUnit.MINUTES);
        /*将随机数画成图片*/
        /*转化为base64*/
        /*返回*/
        return VerifyCodeUtils.VerifyCode(110,40,randomString);
    }

    @Override
    public void smsCode(RegisterDto dto) {
        /*判断是否为空*/
        if(StringUtils.isEmpty(dto.getPhone())
         || StringUtils.isEmpty(dto.getImageKey())
         || StringUtils.isEmpty(dto.getImageCode())
        ){
            throw new BusinessException("你的参数有问题");
        }
        /*检测手机号是否被注册*/
        String phone = dto.getPhone();
        Logininfo longininfoTmp = logininfoMapper.loadByPhone(phone);
        if(longininfoTmp!=null){
            throw new BusinessException("用户已经注册，请登录");
        }
        /*检测图形验证码是否正确，不区分大小写*/
        Object imgCodeTmp = redisTemplate.opsForValue().get(dto.getImageKey());
        if (imgCodeTmp==null){ //检测图形验证码是否存在即失效
            throw new BusinessException("图形验证码失效，请重新填写");
        }
        if (!imgCodeTmp.toString().equalsIgnoreCase(dto.getImageCode())){
            throw new BusinessException("图形验证码错误");
        }
        /*检验短信验证码是否有效*/
        //获取号码存储的key
       String smsCodeKey =  BaseConstant.SmsConstant.BUSINESS_REGISTER_PREFIX+phone;
       Object smsCodeTmp = redisTemplate.opsForValue().get(smsCodeKey);
       String code=StrUtils.getComplexRandomString(4);//生成4位验证码
       if (smsCodeTmp!=null){//获取时间戳
           Long oldtime = Long.valueOf(smsCodeTmp.toString().split(":")[1]);
           //判断获取时间是否超过一分钟
           if (System.currentTimeMillis() - oldtime<=1*60*1000){
               throw new BusinessException("请勿重复获取"); //如果没过报错
           }//时间过了就使用历史的验证码,重新设置过期时间
           code=smsCodeTmp.toString().split(":")[0];
       }
       /*存储Redis发送短信*/
        String CodeTmp = code +":"+System.currentTimeMillis(); //添加时间戳存储
        redisTemplate.opsForValue().set(
                smsCodeKey,
                CodeTmp,
                3,
                TimeUnit.MINUTES
        );
        SmsUtils.sendSmsMsg(phone,"你好，你的验证码是："+code+"请在三分钟内使用");
    }
}
